package javacommon.base;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javacommon.util.RandomNum;
import javacommon.util.DateUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;
/**
 * @author badqiu
 */
@Transactional
public abstract class BaseManager <E,PK extends Serializable>{
	
	protected Log log = LogFactory.getLog(getClass());

	protected abstract EntityDao getEntityDao();
	private static int seq = 10;    //序列,取值范围从10到99；  用于产生任务ID
	
	@Transactional(readOnly=true)
	public E getById(PK id) {
		return (E)getEntityDao().getById(id);
	}
	
	@Transactional(readOnly=true)
	public List<E> findAll() {
		return getEntityDao().findAll();
	}
	//13278870215
	public void saveOrUpdate(E entity) {
		getEntityDao().saveOrUpdate(entity);
	}
	
	public void save(E entity) {
		getEntityDao().save(entity);
	}
	
	@Transactional
	public void batchInsert(List list){
		getEntityDao().batchInsert(list);
	}
	
	public void removeById(PK id) {
		getEntityDao().deleteById(id);
	}
	
	public void update(E entity) {
		getEntityDao().update(entity);
	}
	/** 
	* <p>查询数据<br>
	* @param Map map
	* @return List 
	* @author bzq add
	*/
	public List findList(Map map){
		return getEntityDao().findList(map);
	}
	@Transactional(readOnly=true)
	public boolean isUnique(E entity, String uniquePropertyNames) {
		return getEntityDao().isUnique(entity, uniquePropertyNames);
	}
	
	/**
	 * id 的生成器 
	 * 传递的statement参数 如Product.getId
	 * 在ProductMapper.xml文件中要定义一个id为getId select 语句如:
	 * <select id="getId" resultType="java.lang.String">
     *  SELECT SEQG_PRODUCT_INFO_ID.nextval AS ID FROM DUAL 
     * </select>
     * 陈书华
	 */
	@Transactional(readOnly = true)
	public String idGenerate(String statement) {
		Date date = new java.util.Date();
		SimpleDateFormat sd = new SimpleDateFormat("yyMMdd");
		String dateStr = sd.format(date);
	    String maxId = getEntityDao().findId(statement);
		String  id = dateStr + maxId;
		return id;
	}
	/**
	 * 生成主键ID 日期+时间+四位随机串
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static String getId()  {
		try { 
				return DateUtil.getTimeStr()+RandomNum.password(4);
		} catch (Exception e) {
			// e.printStackTrace();
			return null;
		}
	}
	/**
	 * 生成主键ID 日期+时间+count随机串
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static String getId(int count)  {
		try { 
				return DateUtil.getTimeStr()+RandomNum.password(count);
		} catch (Exception e) {
			// e.printStackTrace();
			return null;
		}
	}
	/**
	 * 生成主键ID 两位小时+两位分钟+两位秒+count随机串
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static String getNumberId(int count)  {
		try { 
				return DateUtil.getTimeStr()+RandomNum.password(count);
		} catch (Exception e) {
			// e.printStackTrace();
			return null;
		}
	}
	/**
	 * 生成主键ID 
	 * 按 时间格式+随机位数 
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static String getId(int count,String format)  {
		try { 
				return DateUtil.getDateStr("yyMM")+RandomNum.password(count);
		} catch (Exception e) {
			// e.printStackTrace();
			return null;
		}
	}	
	 private static synchronized long createTaskID(){
		 
	      java.text.SimpleDateFormat format =
	       new java.text.SimpleDateFormat("yyMMddHHmm");  //用来形成规范的日期格式
	       String curr_time = format.format(new java.util.Date(System.currentTimeMillis()));
	      seq++;
	      if(seq>=100){
	        seq = 10;
	      }
	      String s = curr_time+String.valueOf(seq);

	      return Long.parseLong(s);
	 }
	 
	 
	 public void callOrderStatic(Map map){
	   getEntityDao().callOrderStatic(map);
	 }
	 
	public static void main(String[] aa){
		for(int i=0;i<4;i++){
		System.out.println(getId(1));
		System.out.println("--"+createTaskID());
		}
		
		System.out.println("getNumberId:"+getId(2,"yyMM"));
	}
}
